/*
 * $Id: main.cpp 9ce65bed8ef4 2012/08/26 02:14:51 pgurumur $
 * Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *

 * __version__  = "$Revision: 9ce65bed8ef4 $"
 * __author__   = "$Author: pgurumur $"
 * __modified__ = "$Date: 2012-08-26 02:14:51Z $"
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <iostream>
#include <sstream>
#include <string>

#include "consts.h"
#include "ssh2.h"
#include "log.h"

int main(int, char *[]);
void usage(void);

extern char *__progname;

int
main(int argc, char *argv[])
{
   int port = 0;
   int family = 0;

   std::string hostname;
   std::string username;

   if (argc <= 1)
      usage();
   else {
      int ch = 0;
      while ((ch = getopt(argc, argv, "46u:p:v")) != EOF) {
         switch (ch) {
            case '4':
               family = 4;
               break;

            case '6':
               family = 6;
               break;

            case 'p':
               if (isdigit((int)*optarg))
                  port = (int)strtol(optarg, (char **)NULL, 10);
               break;

            case 'u':
               username = optarg;
               break;

            case 'v':
               std::cout << "Not implemented!" << std::cout;
               break;

            default:
               usage();
               break;
         }
      }

      if ((argc = 1) != optind)
         usage();

      hostname = argv[optind];
   }

   if (hostname.length()) {
      if (username.length() == 0)
         username = getenv("USER");

      if (port == 0)
         port = DefaultSSHPort;

      ssh2 client(hostname, username, port);
      client.Connect(family);
      client.Authenticate();
      client.GetChannel();
   }

   return 0;
}

void
usage()
{
   std::cout << __progname << " [ options ] hostname " << std::endl;
   std::cout << " [ options ] " << std::endl;
   std::cout << "\t -4 [ uses ipv4 ] " << std::endl;
   std::cout << "\t -6 [ uses ipv6 ] " << std::endl;
   std::cout << "\t -p [ uses different port, default: 22 ] " << std::endl;
   std::cout << "\t -u [ username ] " << std::endl;
   exit(1);
}

